﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using MarioArena.World;
using Microsoft.Xna.Framework;

namespace MarioArena.HUD
{
    public class NotificationTray
    {
        private const int width = 400;
        private const int height = 300;
        private const int lineHeight = 20;
        private const int textMarginLeft = 5; //Distance between left side and text;
        private const float transparency = 0.4f; //0 = solid, 1 = transparent
        private const float fadeSpeed = 2.0f; //Speed in seconds with which one line will fade.

        private int screenWidth;
        private int screenHeight;
        private int maximumNotifications; //depends on linesize and height of tray
        
        private Color backgroundColor = Color.Black;
        private Color textColor = Color.White;

        private Texture2D backgroundTexture;
        private SpriteFont font;
        private Rectangle bounds;

        private List<string> messages = new List<string>();
        private List<float> messageTransparacies = new List<float>();
        private int fadingMessageCounter = 0;
        private float leftOverTime = 0;

        public void LoadContent()
        {
            backgroundTexture = new Texture2D(GameManager.GraphicsDevice, 1, 1);
            backgroundTexture.SetData(new[] { Color.White });
            font = GameManager.Content.Load<SpriteFont>("Fonts/Notificationtray");

            screenWidth = GameManager.GraphicsDevice.Viewport.Width;
            screenHeight = GameManager.GraphicsDevice.Viewport.Height;

            this.bounds = new Rectangle(0, screenHeight - height, width, height);
            this.maximumNotifications = height / lineHeight;
        }

        public void Update(GameTime gameTime)
        {
            //only update if messages exist.
            if (this.messages.Count > 0)
            {
                float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
                float fadeFactor = elapsedTime / fadeSpeed;

                if (leftOverTime > 0)
                {
                    this.messageTransparacies[fadingMessageCounter] += leftOverTime;
                    leftOverTime = 0;
                }

                this.messageTransparacies[fadingMessageCounter] += fadeFactor;
                //full transparency reached
                if (this.messageTransparacies[fadingMessageCounter] > 1.0f)
                {
                    if (fadingMessageCounter < this.messages.Count - 1)
                        fadingMessageCounter++;
                    //Retrieve time that was left over after full transparency was reached.
                    leftOverTime = this.messageTransparacies[fadingMessageCounter] - 1.0f;
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            //Move the messages to the bottom by adding extra space if not all messages are set.
            int emptySpace = lineHeight * (this.maximumNotifications - this.messages.Count);

            for (int i = 0; i < messages.Count; i++)
            {
                spriteBatch.DrawString(font, this.messages[i], new Vector2(this.bounds.X + textMarginLeft, this.bounds.Y + (i*lineHeight) + emptySpace), this.textColor * (1-this.messageTransparacies[i]));
            }
        }

        /// <summary>
        /// Adds a notification to the bottom of the notification tray.
        /// </summary>
        /// <param name="notification">Notification to be shown.</param>
        public void AddNotification(string notification)
        {
            this.messages.Add(notification); //add a message
            this.messageTransparacies.Add(0f); //set transparency on 0

            //If notifications are full, remove the first one.
            if (this.messages.Count > this.maximumNotifications)
            {
                this.messages.RemoveAt(0);
                this.messageTransparacies.RemoveAt(0);
            }

            //Reset all transparent notifications.
            for (int i = 0; i < this.messageTransparacies.Count; i++)
                this.messageTransparacies[i] = 0f;
            fadingMessageCounter = 0; //reset fade target to oldest message
        }
    }
}
